package com.tulele.vtool.handler;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.tulele.vtool.model.PlatformSysInfo;
import com.tulele.vtool.model.SysGzh;
import com.tulele.vtool.model.logEntity.CipherLog;
import com.tulele.vtool.model.reqEntity.GetArticleDetailInfoReqBean;
import com.tulele.vtool.model.reqEntity.RsaReqBean;
import com.tulele.vtool.model.respEntity.RespData;
import com.tulele.vtool.model.respEntity.ReturnCode;
import com.tulele.vtool.service.CipherLogService;
import com.tulele.vtool.service.SysGzhService;
import com.tulele.vtool.utils.RsaTool;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class RasHandler {

    @Autowired
    private CipherLogService cipherLogService;

    @Autowired
    private SysGzhService sysGzhService;

    //获取密匙对
    @PostMapping(value = "/VPJEyiahnTz0C5rmg7FwMS8Nbu92e_KG", produces = "application/json;charset=UTF-8")
    public RespData VPJEyiahnTz0C5rmg7FwMS8Nbu92e_KG(@RequestHeader("userId") String userId,@RequestHeader("identifying") String springProfilesActive){
        Map<String, Object> map = new HashMap<String, Object>();
        map = RsaTool.init();
        String publicKey = RsaTool.getPublicKey(map);
        String privateKey =RsaTool.getPrivateKey(map);

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(2);
        resultMap.put("WhV0Ds",publicKey);
        resultMap.put("YN27kw",privateKey);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setData(resultMap);
        respData.setMsg("成功");
        return respData;
    }

    //解密
    @PostMapping(value = "/yuxQab_IpK4dS59gh3AwXNlP2jkGYVtfmRinTLO6E1vH8czF", produces = "application/json;charset=UTF-8")
    public RespData yuxQab_IpK4dS59gh3AwXNlP2jkGYVtfmRinTLO6E1vH8czF(@RequestHeader("userId") String userId,
                                                                     @RequestHeader("identifying") String springProfilesActive,
//                                                                     @RequestParam("KNdSnE") String KNdSnE, //密文
//                                                                     @RequestParam("WhV0Ds") String WhV0Ds, //公钥
//                                                                     @RequestParam("SIGN") String SIGN , //签名
//                                                                     @RequestParam("YN27kw") String YN27kw //私钥
                                                                     @RequestBody RsaReqBean rsaReqBean
                                                                     ) {

        String KNdSnE = rsaReqBean.getKNdSnE(); //密文
        String WhV0Ds = rsaReqBean.getWhV0Ds(); //公钥
        String SIGN = rsaReqBean.getSIGN(); //签名
        String YN27kw = rsaReqBean.getYN27kw();//私钥
        PlatformSysInfo platformSysInfo = sysGzhService.getPlatformSysInfo(springProfilesActive);

        RespData respData = new RespData();
        if (KNdSnE == null || WhV0Ds == null || SIGN == null || YN27kw == null || KNdSnE.isEmpty() || WhV0Ds.isEmpty() || SIGN.isEmpty() || YN27kw.isEmpty()) {
            respData.setCode(ReturnCode.SERVER_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        boolean status = RsaTool.verify(KNdSnE, WhV0Ds, SIGN);
        if (!status) {
            respData.setCode(ReturnCode.SERVER_ERROR);
            respData.setMsg("签名失败");
            return respData;
        }

        Integer i = cipherLogService.checkUse(WhV0Ds);
        if (i != null && i == 1) {
            respData.setCode(ReturnCode.SERVER_ERROR);
            respData.setMsg("密钥对已被使用");
            return respData;
        } else {
            //屏蔽于20210405，暂时不记录密钥对
//            CipherLog cipherLog=new CipherLog();
//            cipherLog.setPubKey(WhV0Ds);
//            cipherLog.setPrivKey(YN27kw);
//            cipherLogService.addCipherLog(cipherLog);
        }

        String deStr1 = RsaTool.decryptByPrivateKey(KNdSnE, YN27kw);

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(2);
        resultMap.put("yF9p2H", deStr1);

        //String[] realmArr = {platformSysInfo.getShareDomainUrl(),platformSysInfo.getFrontDomain(),platformSysInfo.getSysGzh().getRealmResource()};
        String[] realmArr = {platformSysInfo.getShareDomainUrl()};
        int index = (int) (Math.random() * realmArr.length);
        resultMap.put("mainlink", realmArr[index]);
        //log.error("yuxQab_IpK4dS59gh3AwXNlP2jkGYVtfmRinTLO6E1vH8czF  platformSysInfo.getShareDomainUrl()"+platformSysInfo.getShareDomainUrl());

        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setData(resultMap);
        respData.setMsg("成功");
        return respData;
    }

    //签名
    @PostMapping(value = "/n7XUIiMjhPq43m12C8BW0s5caYE6vFGVNb_x", produces = "application/json;charset=UTF-8")
    public RespData n7XUIiMjhPq43m12C8BW0s5caYE6vFGVNb_x(@RequestHeader("userId") String userId,
                                                                     @RequestHeader("identifying") String springProfilesActive,
//                                                                     @RequestParam("KNdSnE") String KNdSnE, //密文
//                                                                     @RequestParam("YN27kw") String YN27kw //私钥
                                                         @RequestBody RsaReqBean rsaReqBean
    ){

        String KNdSnE=rsaReqBean.getKNdSnE(); //密文
        String YN27kw =rsaReqBean.getYN27kw();//私钥

        RespData respData = new RespData();
        if(KNdSnE==null ||  YN27kw==null || KNdSnE.isEmpty() ||  YN27kw.isEmpty()){
            respData.setCode(ReturnCode.SERVER_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        String sign = RsaTool.sign(KNdSnE, YN27kw);
        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(2);
        resultMap.put("SIGN",sign);

        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setData(resultMap);
        respData.setMsg("成功");
        return respData;
    }
}
